
export const CrudUMixin = {
  data() {
    return {
      id: null,
      item: {},
      inlineForm: 'InlineForm',
      getItemFunc: (id) => {
        return new Promise((resolve, reject) => { })
      },
      editItemFunc: (id, item) => {
        return new Promise((resolve, reject) => { })
      },
      updateItemFunc: (target) => { }
    }
  },
  created() {
    this.id = this.$route.params.id
    this.get_item()
  },
  methods: {
    get_item() {
      if (this.id) {
        this.getItemFunc(this.id).then(data => {
          console.log(data)
          if (data.code === 0) {
            let act = data.payload
            this.updateItemFunc(act)
            console.log(this.item)
          } else {
            this.$message.error(data.description)
          }
        })
      } else {
        $this.$message.error('数据ID不存在，请检查后重写')
      }
    },
    handleDoSubmit(dataInsight) {
      let form = this.$refs[this.inlineForm]
      form.valid(valid => {
        if (valid) {
          let item = form.obj;
          this.editDataInsight(dataInsight, item, () => {
            this.editItemFunc(this.id, item).then(data => {
              console.log(data.code, '成功');
              if (data.code === 0) {
                this.$message.success('修改成功!')
                this.$router.go(-1)
              } else {
                this.$message.error(data.description)
              }
            })
          });

        } else {
          this.$message.error('数据填写错误，请按要求填写!')
        }
      })
    },
    editDataInsight(dataInsight, item, func) {
      // if (dataInsight && dataInsight === 'dataInsight') {
      //   return req.post({
      //     url: `${Conf.BASE_SERVER_URL}/mod/remote/dataInsight/insertConfig`,
      //     model: this.getFormData(item),
      //   }).then(addCustomsCode => {
      //     if (addCustomsCode.code === 0) {
      //       func();
      //     } else {
      //       this.$message.error(addCustomsCode.description)
      //     }
      //   })
      // } else {
      //   func();
      // }
    },
    getFormData(item) {
      return {
        customsCode: item.dataInsight,
      };
    }
  }
}
